package com.example.demo.utils;

public class ArrayUtils {
    
    /**
     * 将字符串格式的数组转换为Integer数组
     * @param str 格式为"[1,2,3]"的字符串
     * @return Integer数组
     * @throws IllegalArgumentException 如果字符串格式不正确
     */
    public static Integer[] stringToIntArray(String str) {
        if (str == null || str.length() < 2 || str.charAt(0) != '[' || str.charAt(str.length() - 1) != ']') {
            throw new IllegalArgumentException("Invalid array format");
        }
        
        String content = str.substring(1, str.length() - 1).trim();
        if (content.isEmpty()) {
            return new Integer[0];
        }
        
        String[] parts = content.split(",");
        Integer[] result = new Integer[parts.length];
        
        for (int i = 0; i < parts.length; i++) {
            try {
                result[i] = Integer.parseInt(parts[i].trim());
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Invalid number format: " + parts[i], e);
            }
        }
        
        return result;
    }
}
